# -*- coding = utf-8 -*-
# ipv6头
# 60 04 2F 14 02 28 11 40
# FD 42 00 42 00 42 00 01 11 07 00 FF FE 00 00 00
# FD 42 00 42 00 42 00 01 11 07 00 FF FE 00 FF FF

# udp包
# D2 D3
# F0 B0
# 02 28 AD FF

# 00 01 00 66 00 7F 02 18 C3 01 00 00 12 00 00 2C
# 00 00 FF 02 01 02 02 06 00 00 00 34 09 82 02 00
# C8 29 60 D1 A4 4C 29 7A 31 4A 51 43 63 18 99 21
# 49 00 5A 18 D1 78 49 1C D1 70 00 21 11 71 51 78
# C9 07 50 D4 28 49 59 18 0E 79 F6 07 0C D4 04 26
# 8E 81 C5 26 0E 74 01 26 4E 74 12 78 8A 74 C0 7A
# C8 74 1A E0 C4 AF 00 20 0E 79 01 27 37 43 0F 71
# 08 26 8E 81 C5 26 0E 74 03 26 4E 74 12 78 8A 74
# C0 7A C8 74 88 68 00 0E 08 75 88 68 00 0C 48 75
# 88 68 00 0A 88 75 88 68 C8 75 84 20 18 5C 40 07
# 40 0F 03 28 10 D1 28 7A 28 18 00 7E 00 28 0B D0
# 89 89 65 48 18 18 01 F0 EB FB 29 7A 0C 4A 51 43
# 61 18 B7 22 D2 00 88 52 28 7A 09 49 48 43 20 18
# B7 21 C9 00 42 5A 09 1D 41 18 FF 23 34 33 C0 5C
# 00 09 00 F0 DF FB F1 BD AC 05 00 00 BF 05 00 00
# 74 08 00 00 F1 B5 84 B0 00 27 38 00 69 46 08 71
# 68 46 00 7C 02 90 6C 48 02 99 6C 4A 51 43 46 18
# B7 20 C0 00 34 18 6A 4D A9 6B 08 8A 20 80 02 46
# 33 8E 40 1D 83 42 00 DA AB E0 02 98 80 00 28 18
# 00 90 00 6D 40 1C 00 9B 18 65 C8 7B 04 28 01 D0
# 01 28 15 D1 5F 48 31 18 40 1F 30 18 F9 F7 EC FE
# 20 88 40 1D 20 80 0F 20 20 71 38 00 60 71 00 98
# 00 6D 01 00 09 0C A1 71 01 00 09 0A E1 71 20 72
# 02 20 69 46 08 71 01 AA 01 21 38 46 F9 F7 3D FC
# 68 46 00 79 00 28 34 D0 38 46 F9 F7 C3 FE 01 28
# 2F D0 23 20 30 5C 03 28 2B D1 30 78 80 07 28 D0
# 28 7A 69 46 48 71 68 46 00 7C 28 72 21 88 26 48
# 30 18 01 F0 6D FB 20 80 68 46 40 79 28 72 22 88
# 31 8E 10 00 0A 30 81 42 13 DB 3F 48 31 18 0A 38
# 30 18 F9 F7 A9 FE DB 20 20 71 08 20 60 71 02 46
# 3A 48 31 18 30 00 8D 30 F9 F7 69 FC 20 88 0A 30
# 20 80 A8 6B C0 7B 04 28 40 D0 20 88 7F 28 01 DA
# 01 20 04 E0 FF 28 01 DD 03 20 00 E0 02 20 80 1D
# 80 B2 00 90 22 88 30 8E 00 99 51 18 88 42 2D DB

import socket

# 源ip地址与端口
# src_ip = "fd42:42:42:1:1245:ff:fe00:0"
src_ip = "fe80::4c6a:f242:86d2:1511"
src_port = 0    # 0 表示任意端口

# 目的ip地址与端口
# dst_ip = "fd42:42:42:1:1245:ff:fe00:1"
dst_ip = "fe80::4c6a:f242:86d2:1511"
# dst_port = 0x1234
dst_port = 10000

# 创建套接字
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.bind((src_ip, src_port))

# 获取自动分配的源端口
src_port_assigned = sock.getsockname()[1]

# 需要发送的数据
# 字符串
# data = b"hello this is udp packet"

# hex_data = "112233445566778899AABBCCDDEEFF"

broadcast_data = \
    "00 01 00 66 00 7F 02 18 C3 01 00 00 12 00 00 2C\
    00 00 FF 02 01 02 02 06 00 00 00 34 09 82 02 00\
    C8 29 60 D1 A4 4C 29 7A 31 4A 51 43 63 18 99 21\
    49 00 5A 18 D1 78 49 1C D1 70 00 21 11 71 51 78\
    C9 07 50 D4 28 49 59 18 0E 79 F6 07 0C D4 04 26\
    8E 81 C5 26 0E 74 01 26 4E 74 12 78 8A 74 C0 7A\
    C8 74 1A E0 C4 AF 00 20 0E 79 01 27 37 43 0F 71\
    08 26 8E 81 C5 26 0E 74 03 26 4E 74 12 78 8A 74\
    C0 7A C8 74 88 68 00 0E 08 75 88 68 00 0C 48 75\
    88 68 00 0A 88 75 88 68 C8 75 84 20 18 5C 40 07\
    40 0F 03 28 10 D1 28 7A 28 18 00 7E 00 28 0B D0\
    89 89 65 48 18 18 01 F0 EB FB 29 7A 0C 4A 51 43\
    61 18 B7 22 D2 00 88 52 28 7A 09 49 48 43 20 18\
    B7 21 C9 00 42 5A 09 1D 41 18 FF 23 34 33 C0 5C\
    00 09 00 F0 DF FB F1 BD AC 05 00 00 BF 05 00 00\
    74 08 00 00 F1 B5 84 B0 00 27 38 00 69 46 08 71\
    68 46 00 7C 02 90 6C 48 02 99 6C 4A 51 43 46 18\
    B7 20 C0 00 34 18 6A 4D A9 6B 08 8A 20 80 02 46\
    33 8E 40 1D 83 42 00 DA AB E0 02 98 80 00 28 18\
    00 90 00 6D 40 1C 00 9B 18 65 C8 7B 04 28 01 D0\
    01 28 15 D1 5F 48 31 18 40 1F 30 18 F9 F7 EC FE\
    20 88 40 1D 20 80 0F 20 20 71 38 00 60 71 00 98\
    00 6D 01 00 09 0C A1 71 01 00 09 0A E1 71 20 72\
    02 20 69 46 08 71 01 AA 01 21 38 46 F9 F7 3D FC\
    68 46 00 79 00 28 34 D0 38 46 F9 F7 C3 FE 01 28\
    2F D0 23 20 30 5C 03 28 2B D1 30 78 80 07 28 D0\
    28 7A 69 46 48 71 68 46 00 7C 28 72 21 88 26 48\
    30 18 01 F0 6D FB 20 80 68 46 40 79 28 72 22 88\
    31 8E 10 00 0A 30 81 42 13 DB 3F 48 31 18 0A 38\
    30 18 F9 F7 A9 FE DB 20 20 71 08 20 60 71 02 46\
    3A 48 31 18 30 00 8D 30 F9 F7 69 FC 20 88 0A 30\
    20 80 A8 6B C0 7B 04 28 40 D0 20 88 7F 28 01 DA\
    01 20 04 E0 FF 28 01 DD 03 20 00 E0 02 20 80 1D\
    80 B2 00 90 22 88 30 8E 00 99 51 18 88 42 2D DB"

data = bytes.fromhex(broadcast_data)

# 发送数据
sock.sendto(data, (dst_ip, dst_port))
print(f"src ip = {src_ip} port = {hex(src_port_assigned)}")
print(f"dst ip = {dst_ip} port = {hex(dst_port)}")
print(f"data = {' '.join(format(x, '02x') for x in data)}")

# 关闭套接字
sock.close()